package com.jiantong.lawEnforcing.impl.service;

import com.google.common.base.Throwables;
import com.jiantong.lawEnforcing.impl.dao.OriginFieldDao;
import com.jiantong.lawEnforcing.model.OriginField;
import com.jiantong.lawEnforcing.service.OriginFieldService;
import io.terminus.boot.rpc.common.annotation.RpcProvider;
import io.terminus.common.model.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 元数据说明
 * Created by mz03 on 2017/8/14.
 */
@Slf4j
@Service
@RpcProvider
public class OriginFieldServiceImpl implements OriginFieldService {
    private final OriginFieldDao originFieldDao;

    @Autowired
    public OriginFieldServiceImpl(OriginFieldDao originFieldDao) {
        this.originFieldDao = originFieldDao;
    }

    /**
     * id 查找
     *
     * @param id
     * @return News
     */
    @Override
    public Response<OriginField> finById(Long id) {
        try {
            OriginField originField = originFieldDao.findById(id);
            if (originField == null) {
                log.error("originField (id={}) is not found", id);
                return Response.fail("originField.not.found");
            }
            return Response.ok(originField);
        }catch (Exception e){
            log.error("failed to find originField(id={}),cause:{}",id, Throwables.getStackTraceAsString(e));
            return Response.fail("originField.find.fail");
        }
    }

    /**
     * 创建新闻新单据
     *
     * @param originField 单据对象
     * @return 主键 id
     */
    @Override
    public Response<Long> create(OriginField originField) {
        try {
            originFieldDao.create(originField);
            return Response.ok(originField.getId());
        }catch (Exception e) {
            log.error("failed.to.create.originField:{}", Throwables.getStackTraceAsString(e));
            return Response.fail("create.originField.failed");
        }
    }

    /**
     * 更新新闻的单据
     *
     * @param originField 单据对象
     * @return true or false
     */
    @Override
    public Response<Boolean> update(OriginField originField) {
        try{
            return Response.ok(originFieldDao.update(originField));
        }catch (Exception e){
            log.error("failed.to.update.originField:{}", Throwables.getStackTraceAsString(e));
            return Response.fail("update.originField.failed");
        }
    }

    /**
     * 根据detailed_type查询一组
     *
     * @param originField
     * @return
     */
    @Override
    public Response<List<OriginField>> finByList(OriginField originField) {
        try {
            List<OriginField> list = originFieldDao.list(originField);
            return Response.ok(list);
        } catch (Exception e) {
            log.error("failed.to.find.originFieldList.by.originField cause:{}", Throwables.getStackTraceAsString(e));
            return Response.fail("find.originFieldList.by.originField.failed");
        }
    }
}
